﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Xml.Linq;

namespace Medianamik.Core.Logging
{
    public class NameValueCollectionTableBuilder : TableBuilder 
    {
        private static readonly string[] _ignoredKeys = 
        {
            "ALL_RAW",
            "ALL_HTTP",
            "__VIEWSTATE"
        };

        public NameValueCollection Collection
        {
            get;
            private set;
        }

        public string TableTitle
        {
            get;
            private set;
        }

        public NameValueCollectionTableBuilder(NameValueCollection pCollection, string pTableTitle)
        {
            Collection = pCollection;
            TableTitle = pTableTitle;
        }

        protected override void Build(XElement table)
        {
            table.Add(BuildHeaderRow(TableTitle, 2));

            var keys = Collection.AllKeys.ToList();
            if (keys.Count == 0)
            {
                var row = new XElement("tr");
                row.Add(new XElement("td"
                    , new XAttribute("style", "border:1px solid silver;padding:0.4em;")
                    , new XAttribute("colspan", 2)
                    , "Aucun élément" ));

                table.Add(row);
            }
            else
            {
                var i = 0;
                keys.ForEach((key) =>
                {
                    string value = Collection[key];
                    if (!_ignoredKeys.Contains(key) && !string.IsNullOrEmpty(value))
                    {
                        table.Add(BuildRow(key, value, ++i%2==0));
                    }
                });
            }
            
        }
    }
}
